package com.seculab.admin.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springmodules.validation.commons.DefaultBeanValidator;

import com.seculab.admin.domain.Code;
import com.seculab.admin.domain.User;
import com.seculab.admin.service.CodeService;
import com.seculab.admin.service.UserService;
import com.seculab.admin.util.CustomPaginationInfo;

@Controller
@RequestMapping("/company")
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private CodeService codeService;
	
	@Autowired
	private CustomPaginationInfo customPaginationInfo;
	
	@Autowired
	private DefaultBeanValidator beanValidator;	
	
	
	@RequestMapping("userList")
	public void userList(@ModelAttribute User user, Model model) throws Exception {
		int totalcnt = 0;
		
		user.setCustomPaginationInfo(customPaginationInfo);
		
		List userList = userService.getUserList(user);
		if(userList.size() > 0) {
			totalcnt = ((Long)((Map)userList.get(0)).get("totalcnt")).intValue();
		}
		
		user.getCustomPaginationInfo().setTotalRecordCount(totalcnt);
		model.addAttribute("userList", userList);
		model.addAttribute("paginationInfo", customPaginationInfo);
	}
	
	@RequestMapping("userForm")
	public void userForm(Model model) throws Exception {
		model.addAttribute("user", new User());
	}	
	
	@RequestMapping("addUser")
	public String addUser(@ModelAttribute User user, BindingResult result) throws Exception {
		beanValidator.validate(user, result);
		if(result.hasErrors()) {
			return "company/userForm";
		}
		
		userService.addUser(user);
		return "redirect:userList";
	}


/*	@RequestMapping("userList")
	public String userList(User user, Model model) throws Exception {
		int totalcnt = 0;
		
		user.setCustomPaginationInfo(customPaginationInfo);
		
		List userList = userService.getUserList(user);
		if(userList.size() > 0) {
			totalcnt = ((Long)((Map)userList.get(0)).get("totalcnt")).intValue();
		}		
		
		user.getCustomPaginationInfo().setTotalRecordCount(totalcnt);
		model.addAttribute("userList", userList);
		model.addAttribute("paginationInfo", customPaginationInfo);
		
		return "company/userList";
	}
	
	@RequestMapping("userForm")
	public String userForm(User user, Model model) throws Exception {
		return "company/userForm";
	}	
	
	@RequestMapping("addUser")
	public String addUser(User user, HttpServletRequest request) throws Exception {
		MultipartFile file;
		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
		Map<String, MultipartFile> files = multiRequest.getFileMap();
			
		Iterator<Entry<String, MultipartFile>> iter = files.entrySet().iterator();
			
		if(iter.hasNext()) {
			Entry<String, MultipartFile> entry = iter.next();
			file = entry.getValue();
			
			FileUploadUtil.uploadFile(file, "c:\\");
		}
		
		userService.addUser(user);
		return "redirect:userList";
	}	
	
	@RequestMapping(value="userModifyForm", method=RequestMethod.POST)
	public String userModifyForm(User user, Model model) throws Exception {
		model.addAttribute("modifyUser", userService.getUser(user));
		return "company/userModifyForm";
	}
	
	@RequestMapping(value="modifyUser", method=RequestMethod.POST)
	public String modifyUser(@ModelAttribute User modifyUser, SessionStatus sessionStatus) throws Exception {
		userService.modifyUser(modifyUser);
		sessionStatus.setComplete();
		return "redirect:userList";
	}*/	
	
	@ModelAttribute("codeTypeList")
	public List<Code> getListCodeType() {
		return codeService.getListCodeType();
	}
	
}
